/*
* Copyright (C) Microsoft. All rights reserved.  
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License.  You may obtain a copy 
* of the License at http://www.apache.org/licenses/LICENSE-2.0  
* 
* THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
* WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
* MERCHANTABLITY OR NON-INFRINGEMENT. 
*
* See the Apache License, Version 2.0 for specific language governing 
* permissions and limitations under the License.
*
*/
#include "pch.h"
#include <journal/views/scene_state.h>
#include <journal/views/interfaces.h>

namespace journal { namespace view {

namespace b = baja;
namespace g = baja::graphics;

scene_state::scene_state() : _dirty(true)
{
    // NOTE: the coordinate system and the world matrix are both fixed!
    // set up coordinate system by flipping y and z so z is "up"
    _coordinateSystemMatrix.set(m::vector3d(1, 0, 0), m::vector3d(0, 0, 1), m::vector3d(0, 1, 0));
    _worldMatrix.makeIdentity();
    _camera = std::dynamic_pointer_cast<view::icamera>(camera::create());
    _light = std::dynamic_pointer_cast<view::light>(light::create());
}

void scene_state::setAmbient(const g::color& ambient)
{
    _ambient = ambient;
    _dirty = true;
}

void scene_state::apply(const std::shared_ptr<g::iengine>& graphics)
{
    graphics->setCoordinateSystemMatrix(_coordinateSystemMatrix);
    graphics->pushWorldMatrix(_worldMatrix, false);

    graphics->setAmbientLight(_ambient);
    graphics->setLightEnable(0, g::enabled_state::enabled);
        
    // TOOD: remove silly dirty flag

    _camera->apply(graphics);
    _light->apply(graphics);
}

}} // namespace journal::view